<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\common\controller\Api;


/**
 * 订单接口
 */
class Order extends Api
{
    protected $noNeedLogin = ['epay','complete'];
    protected $noNeedRight = ['*'];

    //订单付款
    public function pay()
    {
        $order_no = input('order_no','');
        $pay_type = input('pay_type','balance');
        $order = \app\api\model\Order::get(['order_no'=>$order_no]);
        if (!$order)$this->error('订单不存在');
        if ($order->pay_status==1)$this->error('订单已付款');
        if ($pay_type=='wechat'){
            $request = request();
            $notifyurl =  $request->root(true)."/api/order/epay";
            $wechatUser = \app\admin\model\User::get($this->auth->id);
            $pay = \addons\epay\library\Service::submitOrder($order['pay_money'],$order_no,'wechat',$order['shop_name'],$notifyurl,'','miniapp',$wechatUser['wechat_open_id']);
            $this->success('',$pay);
        } else {
            $orderModel = new \app\api\model\Order();
            $orderInfo = $orderModel->where(['order_no'=>$order_no,'pay_status'=>0])->find();
            $wechatUser = \app\admin\model\User::get($this->auth->id);
            if ($wechatUser->money<$orderInfo['pay_money']){
                $this->error('余额不足');
            }
            if(!$orderInfo){
               $this->error('已付款');
            }
            $res = $orderModel->notify($order_no);
            $this->success('提交成功',$res);
        }

    }

    //查询最近一分钟是否有被扫付款码未付款订单
    public function waitpay()
    {
        $order = \app\api\model\Order::where(['pay_status'=>0,'create_type'=>1])
            ->where('createtime','>',time()-60)->find();
        $this->success('',$order);
    }

    //订单列表
    public function lists()
    {
        $where = ['o.user_id'=>$this->auth->id,'o.pay_status'=>1];
        $order_status = input('order_status','all');
        if ($order_status!='all'){
            if ($order_status=='pay'){
                //已付款
                $where['o.order_status'] = 1;
            } elseif ($order_status=='apply'){
                //申请退款
                $where['o.order_status'] = 2;
                //$where['o.refund_status'] = 1;
            } elseif ($order_status=='pass'){
                //同意退款
                $where['o.order_status'] = 3;
                //$where['o.refund_status'] = 2;
            } elseif ($order_status=='refuse'){
                //拒绝退款
                $where['o.order_status'] = 4;
                //$where['o.refund_status'] = 3;
            }
        }
        $list = \app\api\model\Order::alias('o')
            ->join('shop s', 's.id=o.shop_id', 'LEFT')
            ->where($where)
            ->field('o.*,s.logo,s.telphone,s.house_number')
            ->order('o.createtime desc')
            ->paginate(10);
        $this->success('',$list);
    }

    public function detail()
    {
        $id = input('id',0);

        $detail = \app\api\model\Order::alias('o')
            ->join('shop s', 's.id=o.shop_id', 'LEFT')
            ->join('shop_category sc', 'sc.id=s.category_id', 'LEFT')
            ->where(['o.user_id'=>$this->auth->id,'o.id'=>$id])
            ->field('o.*,s.logo,s.images,s.telphone,s.house_number,sc.name as category_name')
            ->find();
        if (!$detail){
            $this->error('订单不存在');
        }
        $this->success('',$detail);
    }

    //申请退款页金额和原因
    public function refunddata()
    {
        $id = input('id',0);

        $order_model = new \app\api\model\Order();
        $order_info = \app\api\model\Order::where(['id'=>$id])->find();
        $data = [
            'refund_reason'=>$order_model->refund_reason,
            'pay_money'=>$order_info->pay_money
        ];

        $this->success('',$data);
    }

    //申请退款
    public function refund()
    {
        $id = input('id',0);
        $refund_reason = input('refund_reason','');
        $refund_money = input('refund_money',0);
        $refund_remark = input('refund_remark','');
        $refund_image = input('refund_image','');

        $order_info = \app\api\model\Order::get($id);
        if ($order_info->refund_status == 1){
            $this->error('已申请退款请勿重复提交');
        }
        if (empty($refund_reason))$this->error('请选择退款原因');
        if (empty($refund_money))$this->error('请传退款金额');
        $order_info->order_status = 2;
        $order_info->order_status_name = "退款中";
        $order_info->refund_status = 1;
        $order_info->apply_refund_time = time();
        //$order_info->refund_reason = $refund_reason;
        $order_info->setAttr('refund_reason',$refund_reason);
        $order_info->refund_money = $refund_money;
        $order_info->refund_remark = $refund_remark;
        $order_info->refund_image = $refund_image;

        if ($order_info->save()){
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }
    }

    //回调
    public function epay()
    {
        $paytype = "wechat";
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        $out_trade_no = $data['out_trade_no'];

        $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;

        $orderModel = new \app\api\model\Order();
        $orderInfo = $orderModel->where(['order_no'=>$out_trade_no,'pay_status'=>0])->find();

        if($orderInfo){
            $res = $orderModel->notify($out_trade_no);
        }
        $this->success('提交成功',$res);
    }

    //完成
    public function complete()
    {
        $id = input('id',0);
        $order = \app\api\model\Order::get($id);
        if ($order->order_status!=1){
            $this->error('订单状态不符合');
        }
        if ($order->is_complete==1){
            $this->error('error');
        }
        $order->is_complete = 1;
        $order->complete_time = time();
        $order->save();
        if ($order->cashback_money){
            \app\common\model\User::money($order->cashback_money,$order->user_id,'订单返现',$order->id,$order->shop_id);
        }

        $this->success('成功');
    }
}